package com.info3k.client.commands.address;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.MapType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl3D;
import com.google.gwt.maps.client.control.MapTypeControl;
import com.google.gwt.maps.client.event.MapClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.info3k.client.Info3K;
import com.info3k.client.components.GlassDialogBox;
import com.info3k.client.components.InfoWindow;
import com.info3k.client.components.validators.TextBoxValidator;
import com.info3k.client.domain.Address;
import com.info3k.client.enums.TabTypes;


public class UpdateAddressCommand extends GlassDialogBox implements Command {

	private TextBoxValidator address = new TextBoxValidator();
	private TextBox houseNumber = new TextBox();
	
	private MapWidget map = new MapWidget(); 
	
	private Button addButton = new Button(Info3K.msgs.editAddress());
	
	private Marker marker;
	
	private Info3K info3k;
	
	public UpdateAddressCommand(Info3K info, final Address add){
		this.info3k = info;
		
		address.setText(add.getAddress());
		address.setValid(true);
		houseNumber.setText(add.getHouseNumber());
		marker = new Marker(LatLng.newInstance(add.getLat(), add.getLon()));
		
		addButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(marker!=null && address.isValid()){
					add.setAddress(address.getText());
					add.setHouseNumber(houseNumber.getText());
					add.setLat(marker.getLatLng().getLatitude());
					add.setLon(marker.getLatLng().getLongitude());
					add.setUser(info3k.getLoginManager().getUser());
					Info3K.loader.setVisible(true);
					Info3K.infoService.addAddress(add, new AsyncCallback<Void>(){
						public void onFailure(Throwable caught) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
						}
	
						public void onSuccess(Void result) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.ok(), Info3K.msgs.addressEditedSuccessfully());
							info3k.getTabManager().refresh(TabTypes.LATEST_ADDRESSES_LEFT);
							info3k.getTabManager().refresh(TabTypes.LATEST_ADDRESSES_TOP);
							hide();
						}
					});
				} else InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.pleaseFillAllFields());
			}
		});
		
		setText(Info3K.msgs.editAddress());
		map.setSize("400px", "300px");
		map.addControl(new LargeMapControl3D());
		map.setCurrentMapType(MapType.getSatelliteMap());
		map.setCenter(LatLng.newInstance(43.850488,18.398074), 14);
		map.addControl(new MapTypeControl());
		
		address.setWidth("300px");
		houseNumber.setWidth("90px");
		
		final Label lblAddressHouseNumber = new Label(Info3K.msgs.address() + " / " + Info3K.msgs.houseNumber());
		
		HorizontalPanel horPanel = new HorizontalPanel();
		horPanel.getElement().setId("horPanelNewAddress");
		horPanel.add(address);
		horPanel.add(houseNumber);
		
		content.add(lblAddressHouseNumber);
		content.add(horPanel);
		content.add(map);
		
		map.addOverlay(marker);
		
		address.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				address.setValid(address.getText().length()>0);
			}
		});
		
		map.addMapClickHandler(new MapClickHandler(){
			public void onClick(MapClickEvent event) {
				if(event.getOverlay() instanceof Marker){
					map.removeOverlay(marker);
					marker = null;
				}
				if(marker!=null) map.removeOverlay(marker);
				marker = new Marker(event.getLatLng());
				map.addOverlay(marker);
			}
		});
		
		footer.add(addButton);
		addButton.getElement().getStyle().setProperty("margin", "0 10px 0 0");
		footer.add(closeButton);
	}

	public void execute() {
		center();
	}
}